package com.chenqi.controller;

import com.chenqi.model.dto.CommentDTO;
import com.chenqi.model.entity.Comment;
import com.chenqi.service.CommentService;
import com.chenqi.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 评论控制层
 * @program: ardaicommunity
 * @author: ChenQi
 * @create: 2021-03-2021/3/13-16-18
 */
@RestController
@RequestMapping("/api/v1/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 查询某个帖子的评论
     * @param pid
     * @return
     */
    @GetMapping("/list/{pid}")
    public JsonData list(@PathVariable("pid") Integer pid) {
        List<Comment> commentsByPostId = commentService.findCommentsByPostId(pid);
        return JsonData.BuildSuccess(commentsByPostId, "查询成功");
    }

    @PostMapping("/addComment")
    public JsonData addComment(@RequestBody CommentDTO commentDTO) {
        int i = commentService.addComment(commentDTO);
        return i == 0 ? JsonData.BuildError("添加评论失败") : JsonData.BuildSuccess("添加评论成功");
    }
}
